﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyShopThoiTrang.QuanLyShopThoiTrang_BUS_WS;

namespace QuanLyShopThoiTrang
{
    public partial class QuanLyNhaCungCap : Form
    {
        int iIndex = 0;
        NhaCungCap temp = new NhaCungCap();
        QLSTT_BUSSoapClient ws = new QLSTT_BUSSoapClient();
        public QuanLyNhaCungCap()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ThemNhaCungCap tncc = new ThemNhaCungCap();
            tncc.Show();
            tncc.Set(this);
        }

        public void FillDatagrid()
        {
            NhaCungCap temp = new NhaCungCap();
            temp.MaNhaCungCap = -1;
            List<NhaCungCap> listNCC = ws.NhaCungCap_TraCuu(temp);

            dataGridView1.Rows.Clear();
            int i = 1;
            foreach (NhaCungCap ncc in listNCC)
            {
                dataGridView1.Rows.Add(i++, ncc.NhaCungCap_Ten, ncc.NhaCungCap_SoDienThoai, ncc.NhaCungCap_DiaChi, ncc.NhaCungCap_Email, ncc.NhaCungCap_Website);
                dataGridView1.Rows[i - 2].Tag = ncc;
            }
        }

        private void QuanLyNhaCungCap_Load(object sender, EventArgs e)
        {
            FillDatagrid();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(dataGridView1.SelectedCells.Count == 0)
            {
                MessageBox.Show("Hãy chọn nhà cung cấp để thay đổi");
                return;
            }

            iIndex = dataGridView1.SelectedCells[0].RowIndex;
            temp = new NhaCungCap();
            temp = (NhaCungCap)dataGridView1.Rows[iIndex].Tag;

            ThayDoiThongTinNhaCungCap tdtt = new ThayDoiThongTinNhaCungCap();
            tdtt.Set(this, temp);
            tdtt.ShowDialog();
            //tdtt.Set(this, temp);
        }

        public void Set(NhaCungCap ncc)
        {
            if(ws.NhaCungCap_CapNhat(ncc))
            {
                FillDatagrid();
                //MessageBox.Show("Cập nhật thành công");
            }
            else
            {
                MessageBox.Show("Thất bại");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            TraCuuNhaCungCap tc = new TraCuuNhaCungCap();
            tc.Set(this);
            tc.ShowDialog();
        }

        public void TraCuu(NhaCungCap nccx)
        {
            List<NhaCungCap> listNCC = ws.NhaCungCap_TraCuu(nccx);

            dataGridView1.Rows.Clear();
            int i = 1;
            foreach (NhaCungCap ncc in listNCC)
            {
                dataGridView1.Rows.Add(i++, ncc.NhaCungCap_Ten, ncc.NhaCungCap_SoDienThoai, ncc.NhaCungCap_DiaChi, ncc.NhaCungCap_Email, ncc.NhaCungCap_Website);
                dataGridView1.Rows[i - 2].Tag = ncc;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn muốn xóa những nhà cung cấp đã chọn?", "Xác nhận", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
                {
                    if (0 <= cell.RowIndex && cell.RowIndex < dataGridView1.Rows.Count)
                    {
                        DataGridViewRow row = dataGridView1.Rows[cell.RowIndex];
                        NhaCungCap ncc = (NhaCungCap)row.Tag;

                        ws.NhaCungCap_Xoa(ncc);
                        dataGridView1.Rows.RemoveAt(row.Index);
                    }
                }

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.SetValues(row.Index + 1);
                }

                MessageBox.Show("Thành công");
            }
            else
            {
                return;
            }
        }
    }
}
